package com.bank.Servlets;

import com.bank.dao.impl.InsertDaoImpl;
import com.bank.dao.impl.QueryDaoImpl;
import com.bank.dao.impl.UpDateDaoImpl;
import com.bank.model.Transaction;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/GetMoney")
public class GetMoneyServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String  bankId =request.getParameter("bank_id");
        double money = Double.parseDouble(request.getParameter("money"));


        QueryDaoImpl queryDao=new QueryDaoImpl();
        Double count=queryDao.queryTb_Bankcard(bankId).getCount();


        UpDateDaoImpl upDateDao=new UpDateDaoImpl();
        upDateDao.upDate(count-money,bankId);
        //获取当前交易时间
        Date date=new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
        String newDate=formatter.format(date);
        //设置交易明细
        Transaction transaction=new Transaction();
        transaction.setDatetime(newDate);
        transaction.setTs_money(money);
        transaction.setCard_id(queryDao.queryTb_Bankcard(bankId).getCard_id());
        InsertDaoImpl insertDao=new InsertDaoImpl();
        insertDao.addTransaction(transaction);

        HttpSession session = request.getSession();
        session.setAttribute("Gt_transaction",transaction);
        response.sendRedirect("getmoney.jsp");
    }
}
